package com.yang.project.base.algorithm;

/**
 * 每日一题：价格减免
 */
public class PriceDevice {
    public static void main(String[] args) {
        PriceDevice priceDevice = new PriceDevice();
        String s = "there are $1 $2 and 5$ candies in the shop";
//        String result = priceDevice.discountPrices(s, 50);
        String result = priceDevice.discountPrices2(s, 50);
        System.out.println(result);
    }

    public String discountPrices(String sentence, int discount) {
        //查找符合规范的价格：$数字
        //循环找$符号，检查每个$符号，最多为10为数字‘
        int dollarIndex = 0;          //美元符号的位置
        int nextIndex = 0;          //检索的下标位置
        String number = "";       //数字
        while (dollarIndex < sentence.length()) {
            dollarIndex = sentence.indexOf('$', nextIndex);
            //取美元符号的字符，第一个位置只能是数字，第二个位置职能是数字和空格
            int temp = dollarIndex;
            boolean rule = true;
            while (true) {
                int countIndex = 0;     //计算遍历的数字次数，最多不超过10位
                temp++;
                char[] substring = sentence.substring(temp, temp + 1).toCharArray();
                if (countIndex > 10) {
                    nextIndex = temp;
                    rule = false;
                    break;
                }
                if (temp == dollarIndex + 1) {
                    if (!Character.isDigit(substring[0])) {
                        //这不是一个符合规范的价格
                        nextIndex = temp;
                        rule = false;
                        break;
                    }
                } else {
                    if (!(Character.isDigit(substring[0]) || Character.isSpaceChar(substring[0])) ) {
                        nextIndex = temp;
                        rule = false;
                        break;
                    }
                }
                number += substring;
                countIndex++;
            }
            //如果是规范的数字，计算减免，替换数字
            if (rule) {

            } else {
                //不是规范的数字
                nextIndex = temp;
            }
        }
        return sentence;
    }

    /**
     * 题解：模拟
     */
    public String discountPrices2(String sentence, int discount) {
        //按照空格分割
        String[] strs = sentence.split(" ");
        String[] words = new String[strs.length];
        int count = 0;
        //遍历数组，对每一个字符数组进行检验
        for (String str : strs) {
            if (str.substring(0, 1).equals("$") && isNumber(str.substring(1))) {
                //计算减免
                Double num = Double.valueOf(str.substring(1));
                num = num  * ( 1 - discount / 100.0) ;
                words[count] = "$" + String.format("%.2f", num);
            } else {
                words[count] = str;
            }
            count++;
        }

        StringBuffer stringBuffer = new StringBuffer();
        for (String word : words) {
            stringBuffer.append(word + " ");
        }
        stringBuffer.deleteCharAt(stringBuffer.lastIndexOf(" "));
        return stringBuffer.toString();
    }

    private boolean isNumber(String str) {
        if (str.length() <= 0) {
            return false;
        }
        //全部是数字
        char[] charArray = str.toCharArray();
        for (char c : charArray) {
            if (!Character.isDigit(c)) {
                return false;
            }
        }
        return true;
    }
}
